
const express = require('express');
const app = express();
// 引入暴露的模块
const pool = require('../mysql.js');

// 使用路由方法
const route = express.Router();

// 查询所有数据
route.get('/dynamic',(req,res,next)=>{

    pool.query('select * from user_dynamic', (err, result) => {

        if (err) {
            next(err);
            return
        }


        if (result.length) {
            res.send({ code: 200, msg: '查找成功',data:result})
        } else {
            res.send({ code: 201, msg: '暂无数据'})
        }

    }) 
  
})

// 查询个人动态数据
route.get('/personal',(req,res,next)=>{

    pool.query('select * from user_dynamic where cid=?', [req.query.cid], (err, result) => {

        if (err) {
            next(err);
            return
        }


        if (result.length) {
            res.send({ code: 200, msg: '查找成功',data:result})
        } else {
            res.send({ code: 201, msg: '暂无数据'})
        }

    }) 
  
})

// 上传用户动态
route.post('/uploaddynamic',(req,res,next)=>{
    pool.query('insert into user_dynamic set?',[req.body], (err, result) => {
        if (err) {
            next(err)
            return
        }
        console.log(result)
        res.send({ code: 200, msg: '发布成功' })
    }) 
  
})

// 更新用户头像
route.post('/updateconuserimg', (req, res, next) => {
    console.log(req.body.uid)
    if (req.body.userimg == '') {
        res.send({ code: 201, msg: '输入信息为空' })
        return
    }

    pool.query('update user_dynamic set userimg=? where cid=?', [req.body.userimg, req.body.cid], (err, result) => {
        if (err) {
            next(err)
            return
        }
        res.send({ code: 200, msg: '修改成功' })
    })
})



module.exports = route;